<?php
/**
 * Class Same
 * Desc  不常用控制器
 * Create on 2021/8/3 14:58
 * Create by wangyafang
 */

namespace app\api\controller\xyb;


use addons\xyb\XypHelper;
use \app\api\model\xyb\Carousel;
use \app\api\model\xyb\HelpCenter;
use \app\api\model\xyb\Server;
use app\api\service\UsersService;
use think\Db;
use app\admin\model\xyb\system\Config;

class Same extends Base
{
// 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    private $model;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * desc:轮播列表
     */
    public function carousel_list()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $carouselModel = new Carousel();
        $where['status'] = array('eq', 'normal');
        $data = $carouselModel->getList($carouselModel, $where, 'id,image', 'weigh desc,id desc');
        $this->success('请求成功', $data);
    }

    /**
     * desc:轮播详情
     */
    public function carousel_detail()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $id = $this->request->param('id');
        $data = (new Carousel())->where('id', $id)->field('title,createtime,content')->find();
        $this->success('请求成功', $data);
    }


    /**
     * desc:文章详情
     */
    public function notice_lists()
    {
        $users = $this->getAuthUsers();
        if (!$users_agent = UsersService::getCommunityWhereUsersLocated($users->id)) {
            $this->success('获取成功', []);
        }
        //  $articleModel = new \app\api\model\xyb\AgentArticle();

        $article_lists = \app\api\model\xyb\AgentArticle::getAgentArticleList($users_agent->agent_id, $users_agent->users_identity);
//        $where = ['agent_id' => $users_agent->agent_id, 'status' => array('eq','normal')];
//        $article_lists = $articleModel->getList($articleModel,$where,'id,name as title,createtime','weigh desc,id desc');
        $this->success('请求成功', $article_lists);
    }

    /**
     * desc:文章详情
     */
    public function notice_detail()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $articleModel = new \app\api\model\xyb\AgentArticle();
        $id = $this->request->param('id');
        $data = $articleModel->where('id', $id)->field('id,name as title,createtime,content')->find();
        $this->success('请求成功', $data);
    }


    public static function calculateRefundAmountEachTransaction($order, $refund_num, $ref)
    {
        /*用户支付的金额*/
        $pay_amount = $order['order_amount'];
        /*用户购买的数量*/
        $ticket_num = $order['ticket_num'];
        /*单票的金额*/
        $single_refund_amount = (float)sprintf('%.2f', ($pay_amount / $ticket_num));
        /*退款的金额*/
        $refund_amount = (float)sprintf('%.2f', ($refund_num * $single_refund_amount));

        if(($ref + $refund_amount) > $pay_amount){
            $refund_amount = (float)sprintf('%.2f', ($pay_amount - $ref));
        }
        return $refund_amount;
    }

    /**
     * desc:公告标题/公告内容/用户协议/关于我们/电话客服/微信客服
     */
    public function base_info()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $array = [
//            'notice_title',
//            'notice_content',
            'agreement',
            'about',
            'customer_mobile',
            'receivie_agreement',
            'applet_record_number'
        ];
        $data = array();
        foreach ($array as $k => $v) {
            $data[$v] = $this->configObj->$v;
        }
        $this->success('请求成功', $data);
    }

    public function about()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $value = $this->configObj->about;
        $this->success('请求成功', ['value' => $value]);
    }

    public function agreement()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $value = $this->configObj->agreement;
        $this->success('请求成功', ['value' => $value]);
    }

    public function express_config()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $agent_id = $this->request->param('agent_id');
        $price_rule = (new Server())->where([
            'agent_id' => $agent_id,
            'server_name' => '快递代取',
        ])->value('price_rule');
        $price_rule = json_decode($price_rule, true);
        $express_config = [
            [
                'id' => 1,
                'type' => '小件',
                'price' => $price_rule['price_rule_min'],
                'desc' => $price_rule['desc_rule_min']
            ],
            [
                'id' => 2,
                'type' => '中件',
                'price' => $price_rule['price_rule_middle'],
                'desc' => $price_rule['desc_rule_middle']
            ],
            [
                'id' => 3,
                'type' => '大件',
                'price' => $price_rule['price_rule_max'],
                'desc' => $price_rule['desc_rule_max']
            ],
        ];
        $data['express_config'] = $express_config;
        $this->success('请求成功', $data);
    }

    /**
     * desc:帮助中心列表
     */
    public function help_center_list()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $helpModel = new HelpCenter();
        $data = $helpModel->getList($helpModel, [], 'id,title,createtime', 'weigh desc,id desc');
        $this->success('请求成功', $data);
    }

    /**
     * desc:帮助中心详情
     */
    public function help_center_detail()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $this->success('请求成功', (new HelpCenter())->get($this->request->param('id')));
    }

    /**
     * desc:接单员背景图和公众号图片
     */
    public function receive_apply_img()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $array = [
            'apply_receive_background',
            'public_img',
        ];
        $data = array();
        foreach ($array as $k => $v) {
            $tem = $this->configObj->$v;
            $data[$v] = XypHelper::xyb_full_url($tem);
            unset($tem);
        }
        $data['url'] = array(
            'mode' => 5,
            'path' => $this->configObj->public_url,
            'info' => '公众号跳转链接',
        );
        $data['apply_receive_image_show'] = $this->configObj->apply_receive_image_show;
        $this->success('请求成功', $data);
    }

    /**
     * 意见反馈
     * content反馈内容
     * pic图片信息
     */
    public function feedback_updata()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $app_plat = $this->request->param('app_plat', 'MP-WEIXIN');
        $data['user_id'] = $users->id;
        $data['app_plat'] = $app_plat;
        $data['content'] = $_POST['content'];
        $data['pic'] = $_POST['pic'];
        $data['createtime'] = time();
        Db::name('xyb_feedback')->insert($data);
        $this->success('请求成功', $data);
    }

    /**
     * desc:提现最小金额
     */
    public function min_withdrawal_amount()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $min_withdrawal_amount = $this->configObj->min_withdrawal_amount;
        $withdrawal_pay_method = (new Config())->value('withdrawal_pay_method');
        $this->success('请求成功', ['min_withdrawal_amount' => $min_withdrawal_amount, 'withdrawal_pay_method' => $withdrawal_pay_method]);
    }

    /**
     * desc:接单协议
     */
    public function receivie_agreement()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $receivie_agreement = $this->configObj->receivie_agreement;
        $this->success('请求成功', ['receivie_agreement' => $receivie_agreement]);
    }


}